<?php
/*tre duoi 30phut= 1 lan tre
 tre tu 30 phut den 2 tieng= 1 lan ve som
 2 lan ve som= 1 lan nghi nua ngay
 tre hon 2 tieng tre len=nghi nua ngay*/

/*$mdate=explode("/", $data->val($i,3));
 $usedate=$mdate[2]."-".$mdate[1]."-".$mdate[0];
 $ktdate=getdate(strtotime($usedate));
 if($ktdate[wday]==6){

 }*/

class CheckTime{
	public $DEFAULT_IN_M = '';
	public $DEFAULT_OUT_M = '';
	public $DEFAULT_IN_E = '';
	public $DEFAULT_OUT_E = '';
	public $DEFAULT_OUT_S = '';
	public $DEFAULT_OVER = '';

	public $DEFAULT_M_LATE_30M = '';
	public $DEFAULT_M_LATE_2H = '';
	public $DEFAULT_M_EARLY_30M = '';
	public $DEFAULT_M_EARLY_2H = '';
	public $DEFAULT_E_LATE_30M = '';
	public $DEFAULT_E_LATE_2H = '';
	public $DEFAULT_E_EARLY_30M = '';
	public $DEFAULT_E_EARLY_2H = '';

	public $isSat = "0";

	public $flagDate = "0";
	public $flagIn = "0";
	public $flagOut = "0";

	public $sheetDate = '';
	public $employeeID = "";
	public $employeeName = "";
	public $date = "";
	public $inTime = "0";
	public $outTime = "0";
	public $late = "0";
	public $early = "0";
	public $halfDayOff = "0";
	public $aDayOff = "0";
	public $overtime = "0";
	public $note = "";
	public $update_user="";
	public $update_time="";

	public $id = "";

	function __construct(){
		$this->DEFAULT_IN_M = 8;
		$this->DEFAULT_OUT_M = 11.30;
		$this->DEFAULT_OUT_S = 12;

		$this->DEFAULT_IN_E = 13;
		$this->DEFAULT_OUT_E = 17.30;
		$this->DEFAULT_OVER = 21.30;

		$this->DEFAULT_M_LATE_30M = 8.30;
		$this->DEFAULT_M_LATE_2H = 10;

		$this->DEFAULT_E_LATE_30M = 13.30;
		$this->DEFAULT_E_LATE_2H = 15;

		$this->DEFAULT_E_EARLY_30M = 17;
		$this->DEFAULT_E_EARLY_2H = 15.30;

		$this->DEFAULT_M_EARLY_30M = 11;
		$this->DEFAULT_M_EARLY_2H = 9.30;



	}

	function setUpdateInfo($dname,$dtime){
		$this->update_user=$dname;
		$this->update_time=$dtime;
	}
	
	function Instance($id,$employeeID, $employeeName, $dates, $inTime, $outTime, $late, $early, $halfDayOff, $aDayOff, $overtime, $note){
		$this->id = $id;
		$this->employeeID = $employeeID;
		$this->employeeName = $employeeName;
		$this->date = $dates;
		$this->inTime = $inTime;
		$this->outTime = $outTime;
		$this->late = $late;
		$this->early = $early;
		$this->halfDayOff = $halfDayOff;
		$this->aDayOff = $aDayOff;
		$this->overtime = $overtime;
		$this->note = $note;
	}

	function setDate($sheetDate,$dates){
		$this->sheetDate = $sheetDate;//$this->fomatDate($sheetDate);
		$this->date = $dates;//$this->fomatDate($dates);
		$this->isSat();
	}

	function setDateUpdate($dates){
		$this->date = $dates;
		$this->isSat();
	}

	function checkDate(){
		if ($this->date != $this->sheetDate) {
			$this->flagDate = "1";
		}
	}

	function fomatDate($dates){
		$mdate = explode("/", $dates);
		$usedate = $mdate[2]."-".$mdate[0]."-".$mdate[1];
		return  $usedate;
	}

	function isSat(){
		$ktdate = getdate(strtotime($this->date ));
		$this->isSat = $ktdate[wday];
	}

	function inTime($time){
		try {
			if ($time!='') {
				$temp = explode(':',$time);
				$this->inTime = $temp[0].'.'.$temp[1];
				
				if ($this->inTime > $this->DEFAULT_IN_M) {
					if ($this->inTime < $this->DEFAULT_M_LATE_30M) {
						$this->late = 1;

					}elseif ($this->inTime <= $this->DEFAULT_M_LATE_2H){
						$this->early = 1;

					}elseif ($this->inTime > $this->DEFAULT_M_LATE_2H && $this->inTime <= $this->DEFAULT_IN_E){
						$this->halfDayOff = 1;

					}elseif ($this->inTime < $this->DEFAULT_E_LATE_30M){
						$this->halfDayOff = 1;
						$this->late = 1;

					}elseif ($this->inTime >= $this->DEFAULT_E_LATE_30M && $this->inTime <= $this->DEFAULT_E_LATE_2H){
						$this->halfDayOff = 1;
						$this->early = 1;
					}
					elseif ($this->inTime > $this->DEFAULT_E_LATE_2H){
						$this->aDayOff = 1;
					}
				}
				$this->inTime = $time;
			}
			else{
				$this->flagIn = "1";
			}

		} catch (Exception $e) {

		}
	}

	function outTime($time){
		try {
			if ($time!='') {
				$temp = explode(':',$time);
				$this->outTime = $temp[0].'.'.$temp[1];
				if ($this->isSat=='0') {// sun
					if ($this->outTime >= $this->DEFAULT_OVER) {
						$this->overtime = 3;
					}else if ($this->outTime >= $this->DEFAULT_OUT_E) {
						$this->overtime = 2;
					}else if ($this->outTime >= $this->DEFAULT_OUT_S) {
						$this->overtime = 1;
					}
					
				}else if ($this->isSat=='6') {// sat
					if ($this->outTime < $this->DEFAULT_OUT_S) {
						//
						if ($this->outTime > $this->DEFAULT_OUT_M ) {
							$this->early = 1;

						}elseif ($this->outTime >= $this->DEFAULT_M_LATE_2H ) {
							$this->early = 1;
						}
						elseif ($this->outTime < $this->DEFAULT_M_LATE_2H ) {
							$this->halfDayOff = 1;
						}

					}

					if ($this->outTime >= $this->DEFAULT_OVER) {
						$this->overtime = 2;
					}else if ($this->outTime >= $this->DEFAULT_OUT_E) {
						$this->overtime = 1;
					}
						
				}else{
					if ($this->outTime < $this->DEFAULT_OUT_E) {
						//
						if ($this->outTime > $this->DEFAULT_E_EARLY_30M) {
							$this->early = 1;

						}elseif ($this->outTime >= $this->DEFAULT_E_EARLY_2H) {
							$this->early = 1;

						}elseif ($this->outTime < $this->DEFAULT_E_EARLY_2H && $this->outTime >= $this->DEFAULT_OUT_M ) {
							$this->halfDayOff = 1;

						}elseif ($this->outTime > $this->DEFAULT_M_EARLY_30M ) {
							$this->halfDayOff = 1;
							$this->early = 1;

						}elseif ($this->outTime > $this->DEFAULT_M_EARLY_2H ) {
							$this->halfDayOff = 1;
							$this->early = 1;
						}
						elseif ($this->outTime < $this->DEFAULT_M_EARLY_2H ) {
							$this->aDayOff = 1;
						}

					}

					if ($this->outTime >= $this->DEFAULT_OVER) {
						$this->overtime = 1;
					}
				}
				$this->outTime = $time;

			}else{
				$this->flagOut = "1";
			}

		} catch (Exception $e) {

		}
	}

	function off($in, $out,$l_confirm_excel){
		if ($in == '' && $out == '') {
			if ($this->isSat=='0') {
				return;
			}
			elseif ($this->isSat=='6') {
				$this->halfDayOff = 1;
			}else{
				$this->aDayOff = 1;
			}
		}elseif ($in == '' || $out == ''){
			$this->note = $l_confirm_excel;
		}
	}
}
?>